var dataGridLoading = $("#dataGridLoading");
//console.info(dataGridLoading);
var dataGrid = $("#dataGrid");
//console.info(dataGrid);
var pagination = $("nav > ul.pagination");
//console.info(pagination);
//console.info(document.querySelectorAll("nav > ul.pagination"));
var trTemplate = dataGrid.children(":eq(0)");

var searchForm = $("#searchForm");

/**
 * 格式化数据表格
 * @param {Object} list
 */

var formatData = function(list) {
	dataGrid.empty();
	$.each(list, function(i, obj) {
		let tr = trTemplate.clone().appendTo(dataGrid);
		obj.rowNum = i + 1;
		$.each(obj, function(name, value) {
			tr.find("._" + name).text(value);
		});
		tr.find("._del").click(function() {
			del(obj.newsId);
		});
		tr.find("._update").click(function() {
			upd(obj.newsId);
		});
	});
}

var updForm = $("#updateForm");
var nowId ;
var  upd=function(newId){
	//var classList = updForm.find("[name='newsClassId']");	
	
	var classLists = updForm.find("[name='newsClassId']");
	classLists.empty();

	var id = newId;
	
	$.post(
			"web2/news-update.s",
			{
				newsId:id
			},
			function(data){ 	
					var r = eval(data);
					//$(".myTitle").val(content.newsTitle);
					$("#a").val(r.newsTitle);
					$("#b").val(r.newsWorker);
					$("#d").val(r.newsContent);
					$("#c").val(r.newsClassId);
					$("#e").val(r.newsId);
					var num = r.newsClassId;
//					document.getElementsById("a")[0].value =r.newsTitle;
//					document.getElementsById("b")[0].value =r.newsWorker;
//					document.getElementsById("d")[0].value =r.newsContent;
//					document.getElementsById("c")[0].value =r.newsClassId; //类别
//					document.getElementsById("e")[0].value =r.newsId;
					var type=r.newsId;
					$.get(
							"web2/news-class-list.s",
							function(data){
								$.each(data,function(i,item){
									if(num!=item.newsClassId){
									$("<option></option>")
										.val(item.newsClassId)
										.text(item.newsClassName)
										.appendTo(classLists);
									}
									else{
										$("<option selected='selected' ></option>")
										.val(item.newsClassId)
										.text(item.newsClassName)
										.appendTo(classLists);
										
									}
									
								});
								
							},"json"
						);
					
					
					 nowId = r.newsClassId;
				
			},"json"
		);	
}

updForm.submit(function(event){
	
	$.post(
		"web2/news-updates.s",
		updForm.serializeArray(),
		function(data){
			if(data=="ok"){
				$("#updateDialog").modal("hide");
				updForm.trigger("reset");
				loadData();
			}else{
				alert(data);
			}
		},"json"
	);
	return false;
});


var delDialog = $("#delDialog");
var delNewsId = 0;
delDialog.find("._confirm").click(function() {
	$.get(
		"web2/news-del.s", {
			newsId: delNewsId
		},
		function(data) {
			if(data == "ok") {
				loadData();
			} else {
				alert(data);
			}
		}, "json");

});

var del = function(newsId) {
	
	delNewsId = newsId;
	delDialog.modal("show");
}

var liTemplate = pagination.children(":eq(1)");

/**
 * 格式化页码
 * @param {JSON} p
 */
var formatPagination = function(p) {
	pagination.show();
	pagination.children(":not(:eq(0),:eq(-1))").remove();

	$.each(p.navigatepageNums, function(i, v) {
		var li = liTemplate.clone();
		li.children("a").text(v);
		li.attr("data-page", v);
		if(p.pageNum == v) {
			li.addClass("active");
		}
		pagination.children(":last").before(li);
	});
	pagination.children(":last").attr("data-page", p.pages);
}

var cur = 1;
/**
 * 加载服务器端json数据
 */
var param = [];
var loadData = function(current) {
	if(current == null) {
		current = cur;
	}
	pagination.hide();
	dataGridLoading.show();
	dataGrid.hide();

	var param1 = param.concat(
		[{
			name: "current",
			value: current
		}, {
			name: "rowMax",
			value: 5
		}]);


	$.get(
		"web2/news-list.s",
		param1,
		function(data) {
			cur = data.pageNum;
			formatPagination(data);
			formatData(data.list);
			dataGridLoading.hide();
			dataGrid.show();
		}, "json"
	);

}
loadData();

pagination.on("click", "[data-page]", function() {
	loadData($(this).data("page"));
});

searchForm.submit(function() {
	param = searchForm.serializeArray();
	loadData(1);
	return false;
});


var addForm = $("#addForm");
var classList = addForm.find("[name='newsClassId']");
$.get(
	"web2/news-class-list.s",
	function(data){
		$.each(data,function(i,item){
			$("<option ></option>")
				.val(item.newsClassId)
				.text(item.newsClassName)
				.appendTo(classList);
		});
		
	},"json"
);

addForm.submit(function(event){
	$.post(
		"web2/news-add.s",
		addForm.serializeArray(),
		function(data){
			if(data=="ok"){
				$("#addDialog").modal("hide");
				addForm.trigger("reset");
				loadData();
			}else{
				alert(data);
			}
		},"json"
	);
	return false;
});